import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.ArrayList;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

public class Server {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.setProperty("java.rmi.server.codebase", Product.class
				.getProtectionDomain().getCodeSource().getLocation().toString());
		Properties iiop = new Properties();
		iiop.put("java.naming.factory.initial",
				"com.sun.jndi.cosnaming.CNCtxFactory");
		iiop.put("java.naming.provider.url", "iiop://localhost/");

		try {
			System.setProperty("java.security.policy", "policy.policy");
			System.setSecurityManager(new RMISecurityManager());
			LocateRegistry.createRegistry(1099);
			
			//Maak Saxtiehuis
			SaxtiehuisImpl huis = new SaxtiehuisImpl();

			Context namingContext = new InitialContext(iiop);
//			PortableRemoteObject.unexportObject(huis);
			PortableRemoteObject.exportObject(huis);
			
			namingContext.rebind("huis", (Object)PortableRemoteObject.toStub(huis));
			
			huis.setProducten(maakProducten(namingContext));
			
			while(true){}
			
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}
	
	public static ArrayList<Product> maakProducten(Context context) throws RemoteException{
		ArrayList<Product> producten = new ArrayList<Product>();
		Product p1 = new ProdImpl(1, "Laptop", 300);
		Product p2 = new ProdImpl(2, "Toetsenbord", 20);
		Product p3 = new ProdImpl(3, "Muis", 10);
		Product p4 = new ProdImpl(4, "Adapter", 25);
		
		producten.add(p1);
		producten.add(p2);
		producten.add(p3);
		producten.add(p4);
		
		return producten;
	}
}
